package com.enjoyor.health.HGPT.web.rest;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.enjoyor.health.HGPT.core.dto.ReservationWeekDto;
import com.enjoyor.health.HGPT.core.entity.PhysicalExaminationPackage;
import com.enjoyor.health.HGPT.core.entity.UserInfo;
import com.enjoyor.health.HGPT.core.service.IPhysicalExaminationPackageService;
import com.enjoyor.health.HGPT.service.BizReservationService;
import com.enjoyor.health.HGPT.web.CodeEnum;
import com.enjoyor.health.HGPT.web.RetResult;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * @author Zhu Min
 * @date 2023/10/19 15:14
 */
@Api(value = "预约", tags = {"预约"})
@Slf4j
@RestController
public class ReservationController {

    @Autowired
    private BizReservationService bizReservationService;
    @Autowired
    private IPhysicalExaminationPackageService physicalExaminationPackageService;

    /**
     *  预约
     */
    @RequestMapping(value = "/aiHealthReport/userInfoShow", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult miniappSchedule(String openId){
        UserInfo userInfo = bizReservationService.getUserInfoByOpenId(openId);
        if (userInfo != null){
            JSONObject result = new JSONObject();
            result.put("name", userInfo.getName());
            if (StringUtils.isNotBlank(userInfo.getIdNumber()) && userInfo.getIdNumber().length()>14){
                result.put("idNumber", userInfo.getIdNumber().substring(0,6) + "********" + userInfo.getIdNumber().substring(14));
            }else {
                result.put("idNumber", userInfo.getIdNumber());
            }
            result.put("gender", (userInfo.getGender() != null && userInfo.getGender()==1) ? "女":"男");
            result.put("height", userInfo.getHeight());
            result.put("weight", userInfo.getWeight());
            if (StringUtils.isNotBlank(userInfo.getPhone()) && userInfo.getPhone().length() == 11){
                result.put("phone", userInfo.getPhone().substring(0,3) + "****" + userInfo.getPhone().substring(7));
            }else {
                result.put("phone", userInfo.getPhone());
            }
            RetResult retResult = new RetResult().of(CodeEnum.SUCCESS);
            retResult.setResult(result);
            return retResult;
        }else {
            return new RetResult().of(CodeEnum.NO_USER_INFO);
        }
    }

    /**
     * 预约信息
     */
    @RequestMapping(value = "/aiHealthReport/reservationInfo", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult miniappScheduleinfo(String openId){
        try {
            RetResult retResult = new RetResult().of(CodeEnum.SUCCESS);
            retResult.setResult(bizReservationService.getScheduleInfo(openId));
            return retResult;
        }catch (Exception e){
            log.error("获取预约信息出错",e);
            return new RetResult().of(CodeEnum.SERVER_EXCEPTION);
        }
    }

    /**
     * 预约结果
     */
    @RequestMapping(value = "/aiHealthReport/reservation", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult miniappReservation(String openId, String date, String period, String packageType){
        JSONObject ret = bizReservationService.getDbAppointment(openId, date, period, packageType);
        JSONObject number = new JSONObject();
        number.put("reservationNumber", ret.get("number"));
        return new RetResult().of(ret.getObject("code", CodeEnum.class)).setResult(number);
    }

    /**
     * 可预约的信息-7天
     */
    @RequestMapping(value = "/aiHealthReport/reservationAvailable", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult reservationAvailableInfo(String openId){
        JSONArray available = bizReservationService.reservationAvailableSevenDay(openId);
        JSONObject result = new JSONObject();
        result.put("available", available);
        return new RetResult().of(CodeEnum.SUCCESS).setResult(result);
    }

    /**
     * 可预约的信息-单天
     */
    @RequestMapping(value = "/aiHealthReport/reservationAvailableDate", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult reservationAvailableDateInfo(String openId, String date){
        JSONArray available = bizReservationService.reservationAvailableSingleDay(date);
        JSONObject result = new JSONObject();
        result.put("available", available);
        return new RetResult().of(CodeEnum.SUCCESS).setResult(result);
    }

    /**
     * 可预约的信息-7天, V2按体验包类型
     */
    @RequestMapping(value = "/aiHealthReport/reservationAvailableV2", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult reservationAvailableInfoV2(String openId, String packageType){
        JSONArray available = bizReservationService.reservationAvailableSevenDayV2(openId, packageType);
        LocalDate ld = LocalDate.now();
        JSONObject result = new JSONObject();
        result.put("available", available);
        result.put("year", ld.getYear());
        result.put("month", ld.getMonthValue());
        return new RetResult().of(CodeEnum.SUCCESS).setResult(result);
    }

    /**
     * 可预约的信息-单天, V2按体验包类型
     */
    @RequestMapping(value = "/aiHealthReport/reservationAvailableDateV2", method = { RequestMethod.GET, RequestMethod.POST })
    public RetResult reservationAvailableDateInfoV2(String openId, String date, String packageType){
        JSONArray available = bizReservationService.reservationAvailableSingleDayV2(date, packageType);
        JSONObject result = new JSONObject();
        result.put("available", available);
        return new RetResult().of(CodeEnum.SUCCESS).setResult(result);
    }

}
